package lg.minecraft.plugin.pokemonmenu.gui.edit;

import com.cobblemon.mod.common.api.pokemon.Natures;
import com.cobblemon.mod.common.pokemon.Nature;
import com.cobblemon.mod.common.pokemon.Pokemon;
import lg.minecraft.plugin.library.inventory.CancelInventoryContext;
import lg.minecraft.plugin.library.inventory.ClickInventoryContext;
import lg.minecraft.plugin.library.inventory.InventoryContext;
import lg.minecraft.plugin.library.inventory.LGInventory;
import lg.minecraft.plugin.pokemonmenu.LGPokemonMenu;
import lg.minecraft.plugin.pokemonmenu.gui.EditInventor;
import org.bukkit.Sound;
import org.bukkit.entity.Player;

import java.util.HashMap;
import java.util.Map;

/**
 * @author langle__
 * @version 1.0
 */
public class EditNatureInventor extends LGInventory {

    private static final int[] GLASS_ARRAY = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 17, 18, 26, 27, 35, 36, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53};
    private static final Map<Integer, Nature> NATURE_MAP = new HashMap<>();

    static {
        NATURE_MAP.put(10, Natures.INSTANCE.getLONELY());
        NATURE_MAP.put(11, Natures.INSTANCE.getBRAVE());
        NATURE_MAP.put(19, Natures.INSTANCE.getADAMANT());
        NATURE_MAP.put(20, Natures.INSTANCE.getNAUGHTY());
        NATURE_MAP.put(28, Natures.INSTANCE.getMODEST());
        NATURE_MAP.put(29, Natures.INSTANCE.getMILD());
        NATURE_MAP.put(37, Natures.INSTANCE.getQUIET());
        NATURE_MAP.put(38, Natures.INSTANCE.getRASH());
        NATURE_MAP.put(15, Natures.INSTANCE.getBOLD());
        NATURE_MAP.put(16, Natures.INSTANCE.getRELAXED());
        NATURE_MAP.put(24, Natures.INSTANCE.getIMPISH());
        NATURE_MAP.put(25, Natures.INSTANCE.getLAX());
        NATURE_MAP.put(33, Natures.INSTANCE.getCALM());
        NATURE_MAP.put(34, Natures.INSTANCE.getGENTLE());
        NATURE_MAP.put(42, Natures.INSTANCE.getSASSY());
        NATURE_MAP.put(43, Natures.INSTANCE.getCAREFUL());
        NATURE_MAP.put(12, Natures.INSTANCE.getTIMID());
        NATURE_MAP.put(14, Natures.INSTANCE.getHASTY());
        NATURE_MAP.put(30, Natures.INSTANCE.getJOLLY());
        NATURE_MAP.put(32, Natures.INSTANCE.getNAIVE());
        NATURE_MAP.put(22, Natures.INSTANCE.getSERIOUS());
    }

    private final Pokemon pokemon;

    public EditNatureInventor(Pokemon pokemon) {
        super(6, LGPokemonMenu.getMessage().getGuiTitleEditNature());
        this.pokemon = pokemon;
    }

    @Override
    public void onInventoryOpen(CancelInventoryContext context) {
        for (int slot : GLASS_ARRAY) inventory.setItem(slot, LGPokemonMenu.getGuiItem().getBorder());

        // 攻击
        inventory.setItem(10, LGPokemonMenu.getGuiItem().getNatureLonely());
        inventory.setItem(11, LGPokemonMenu.getGuiItem().getNatureBrave());
        inventory.setItem(19, LGPokemonMenu.getGuiItem().getNatureAdamant());
        inventory.setItem(20, LGPokemonMenu.getGuiItem().getNatureNaughty());
        // 特攻
        inventory.setItem(28, LGPokemonMenu.getGuiItem().getNatureModest());
        inventory.setItem(29, LGPokemonMenu.getGuiItem().getNatureMild());
        inventory.setItem(37, LGPokemonMenu.getGuiItem().getNatureQuiet());
        inventory.setItem(38, LGPokemonMenu.getGuiItem().getNatureRash());
        // 防御
        inventory.setItem(15, LGPokemonMenu.getGuiItem().getNatureBold());
        inventory.setItem(16, LGPokemonMenu.getGuiItem().getNatureRelaxed());
        inventory.setItem(24, LGPokemonMenu.getGuiItem().getNatureImpish());
        inventory.setItem(25, LGPokemonMenu.getGuiItem().getNatureLax());
        // 特防
        inventory.setItem(33, LGPokemonMenu.getGuiItem().getNatureCalm());
        inventory.setItem(34, LGPokemonMenu.getGuiItem().getNatureGentle());
        inventory.setItem(42, LGPokemonMenu.getGuiItem().getNatureSassy());
        inventory.setItem(43, LGPokemonMenu.getGuiItem().getNatureCareful());
        // 速度
        inventory.setItem(12, LGPokemonMenu.getGuiItem().getNatureTimid());
        inventory.setItem(14, LGPokemonMenu.getGuiItem().getNatureHasty());
        inventory.setItem(30, LGPokemonMenu.getGuiItem().getNatureJolly());
        inventory.setItem(32, LGPokemonMenu.getGuiItem().getNatureNaive());
        // 平衡
        inventory.setItem(22, LGPokemonMenu.getGuiItem().getNatureSerious());

        inventory.setItem(49, LGPokemonMenu.getGuiItem().getBack());
    }

    @Override
    public void onInventoryClick(ClickInventoryContext context) {
        context.setCancelled(true);
        Player player = context.getPlayer();
        int slot = context.getSlot();

        if (slot == 49) {
            new EditInventor(pokemon).openInventory(player);
            player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 0.5F, 1F);
        } else if (NATURE_MAP.containsKey(slot)) {
            pokemon.setNature(NATURE_MAP.get(slot));
            player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1.0F, 1.8F);
        }
    }

    @Override
    public void onInventoryClose(InventoryContext context) {

    }

}
